package rexsee.device;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import android.content.Context;
import android.os.Vibrator;

public class RexseeVibrate implements JavascriptInterface {

	public static final String INTERFACE_NAME = "Vibrate";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeVibrate(childBrowser);
	}

	private final Context mContext;
	private final Browser mBrowser;

	public RexseeVibrate(Browser browser) {
		super();
		mBrowser = browser;
		mContext = browser.getContext();
	}

	//JavaScript Interface
	public void vibrate() {
		((Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(25);
	}
	public void vibrate(String pattern) {
		String[] tmp = pattern.split("\\|");
		if (tmp.length == 1) {
			((Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(Integer.parseInt(tmp[0]));

		} else {
			long[] p = new long[tmp.length];
			for (int i = 0; i < p.length; i++) {
				p[i] = Long.parseLong(tmp[i]);
			}
			((Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(p, -1);
		}
	}

}
